﻿using Microsoft.VisualBasic;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
public class Solution20
{
    public bool IsValid(string s)
    {
        Dictionary<char, char> dic = new Dictionary<char, char>();
        dic.Add('(', ')');
        dic.Add('[', ']');
        dic.Add('{', '}');


        Stack<char> stack = new Stack<char>();


        for (int i = 0; i < s.Length; i++)
        {
            char c = s[i];

            if (dic.ContainsKey(c))
            { //入栈，左括号

                stack.Push(c);



            }
            else//遇到右括号，与栈顶匹配
            {

                if (stack.Count > 0)
                {
                    char top = stack.Peek();
                    if (dic[top] == c)
                    {//如果是对应的左括号 

                        stack.Pop();//出栈

                    }
                    else
                    {
                        return false;
                    }

                }
                else//栈空，遇到右括号
                {
                    return false;
                }


               



            }

        }

        if (stack.Count > 0) { return false; }
        return true;

    }
}

    class Program20
    {

        static void Main20(string[] args)
        {
            string s = "{[]}";

            Solution20 solution = new Solution20();
            bool res = solution.IsValid(s);
            Console.WriteLine(res);
        }
    }
